import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class CommonPicker {
  static Future<int?> showPicker({
    required BuildContext context,
    required List<String> options,
    int? value,
    double height = 300.0,
  }) {
    var controller = FixedExtentScrollController(initialItem: value ?? 0);
    return showCupertinoModalPopup<int>(
      context: context,
      builder: (BuildContext context) {
        return Container(
          height: height,
          decoration: BoxDecoration(color: Colors.grey[200]),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                height: 50.0,
                decoration: BoxDecoration(color: Colors.white),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    TextButton(
                      onPressed: () => Navigator.of(context).pop(),
                      child: Text("取消"),
                    ),
                    TextButton(
                      onPressed: () => Navigator.of(context).pop(controller.selectedItem),
                      child: Text("确定"),
                    ),
                  ],
                ),
              ),
              Expanded(
                child: CupertinoPicker.builder(
                  scrollController: controller,
                  childCount: options.length,
                  itemExtent: 44.0,
                  onSelectedItemChanged: (index) {
                    value = index;
                  },
                  itemBuilder: (context, index) => Text(options[index].toString()),
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}
